package com.godGuang.servser.impl;

import com.godGuang.domain.Review;
import com.godGuang.mapper.ReviewManager;
import com.godGuang.servser.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

import static com.godGuang.config.UniformConstant.PATIENTS;


/**
 * 评论服务实现类
 */


@Service
public class ReviewServiceImpl implements ReviewService {

    @Autowired
    private ReviewManager reviewManager;

    @Autowired
    private ContentServiceImpl contentService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 添加评论
     *
     * @param review 评论对象
     */
    @Override
    public void reviewAdd(Review review) {

        char[] charArray = review.getReviewContent().toCharArray();
        String str = "";
        for (char c : charArray) {
            String text = contentService.desensitizeText(String.valueOf(c));
            str += text;
        }
        review.setReviewContent(str);
        review.setCreationTime(new Date());
        review.setDelete(0);
        if (null == review.getContentId()){
            review.setReviewIn(String.valueOf(review.getReviewId()));
        }
        reviewManager.insert(review);
        reviewManager.inserts(review.getContentId(),review.getReviewId());
        redisTemplate.delete(PATIENTS);
    }

    /**
     * 获取指定内容的评论列表
     *
     * @param contentId 内容ID
     * @return 评论列表
     */
    @Override
    public List<Review> reviewList(String contentId) {
        return reviewManager.reviewList(contentId);
    }
}


